package com.partsoft.dits.message;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 数据操作
 */
public enum DataAction {
	
	INSERT(1),
	
	UPDATE(2),
	
	DELETE(3),
	
	GET(0);
	
	private final int code;
	
	public int getCode() {
		return code;
	}
	
	private DataAction(int code) {
		this.code = code;
	}

	public static final DataAction fromName(String name) {
		if (!StringUtils.hasText(name)) return null;
		name = name.trim().toUpperCase();
		for (DataAction option : DataAction.values()) {
			if (ObjectUtils.nullSafeEquals(name, option.name())) 
				return option;
		}
		return null;
	}
	
	public static final DataAction fromCode(int code) {
		for (DataAction action : DataAction.values()) {
			if (code == action.getCode()) return action;
		}
		return null;
	}
}
